<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Zhishidi_Newarrival_Block_Sidebar extends Mage_Catalog_Block_Product_Abstract {
    const SIDEBAR_CACHE_KEY = 'zhishidi_newarrival_sidebar';
    const XML_NEW_ARRIVAL_MODEL_PATH = 'zhishidi_newarrival/settings';
    protected $XML_ZHISHIDI_NEWARRIVAL_CONFIG_PATH = 'zhishidi_newarrival_info/sidebar/';

    protected function _construct() {
        parent::_construct();
        $this->addData(array(
            'cache_lifetime' => 86400,
            'cache_tags' => array(self::SIDEBAR_CACHE_KEY),
        ));
    }

    protected function _beforeToHtml() {
        if ($this->getModel()->isEnabled()) {
            $collection = Mage::getResourceModel('catalog/product_collection');
            $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());

            $collection = $this->_addProductAttributesAndPrices($collection)
                            ->addStoreFilter()
                            ->setPageSize($this->getModel()->getSidebarLimit())
                            ->setCurPage(1)
                            ->setOrder('created_at', 'desc');

            $this->setProductCollection($collection);
        }
        return parent::_beforeToHtml();
    }

    public function getModel() {
        return Mage::getModel(self::XML_NEW_ARRIVAL_MODEL_PATH);
    }

}

?>
